package com.ctms.system.controller;

import com.ctms.common.utils.PageUtils;
import com.ctms.common.utils.Query;
import com.ctms.common.utils.R;
import com.ctms.common.utils.ShiroUtils;
import com.ctms.system.domain.CommitTaskDO;
import com.ctms.system.domain.TaskReleaseDO;
import com.ctms.system.domain.UserDO;
import com.ctms.system.service.TaskReleaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/ToDoTaskRelease")
@Controller
public class ToDoTaskReleaseController {

    @Autowired
    TaskReleaseService taskReleaseService;

    @GetMapping("")
    public String toDoTask(){
        return "/sys/taskRelease/toDoTaskRelease";
    }
    @GetMapping("/list")
    @ResponseBody
    PageUtils list(@RequestParam Map<String,Object> params){
        //获取登陆人
        UserDO userDO = ShiroUtils.getUser();
        int loginId = Integer.parseInt(Long.toString(userDO.getUserId()));
        params.put("loginId",loginId);
        Query query = new Query(params);
        List<TaskReleaseDO> taskReleaseDOS = taskReleaseService.selectAllToDoTask(params);
        List<CommitTaskDO> commitTaskDOS = taskReleaseService.selectCommitByUserId(loginId);
        //已提交的作业flag置为1，未提交置为0
        for (int i=0;i<taskReleaseDOS.size();i++){
            taskReleaseDOS.get(i).setFlag(0);
            for (int j=0;j<commitTaskDOS.size();j++){
                if (taskReleaseDOS.get(i).getTid() == commitTaskDOS.get(j).getTid()){
                    taskReleaseDOS.get(i).setFlag(1);
                }
            }
            System.out.println("******************"+taskReleaseDOS.get(i).getFlag());
        }
        int total = taskReleaseService.toDoCount(query);
        PageUtils pageUtils = new PageUtils(taskReleaseDOS,total);
        return pageUtils;
    }

    //提交作业
    @GetMapping("/commitTask/{id}")
    public String commit(Model model, @PathVariable("id")Integer id){
        //任务id
        int tid = id;
        model.addAttribute("tid",tid);
        return "/sys/taskRelease/commitTask";
    }

    @PostMapping("/save")
    @ResponseBody
    R insert(CommitTaskDO commitTaskDO){
        //获取任务id
        int tid = commitTaskDO.getTid();
        //获取登录人信息，即提交作业学生信息
        UserDO userDO = ShiroUtils.getUser();
        int userId = Integer.parseInt(Long.toString(userDO.getUserId()));
        //时间格式转化
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        String time = simpleDateFormat.format(date);
        //判断是否是第一次提交作业
        if (taskReleaseService.commitCount(tid,userId)==0){
            //第一次提交
            commitTaskDO.setUserId(userId);
            commitTaskDO.setCommitTime(time);
            //将作业信息插入作业信息表
            if (taskReleaseService.commitTask(commitTaskDO)>0){
                return R.ok();
            }else {
                return R.error();
            }
        }else {
            //不是第一次提交，直接更新
            commitTaskDO.setCommitTime(time);
            CommitTaskDO commitTaskDO1 = taskReleaseService.selectByTidUserId(tid,userId);
            int id = commitTaskDO1.getId();
            commitTaskDO.setId(id);
            //更新作业内容
            if (taskReleaseService.updateCommit(commitTaskDO)>0){
                return R.ok();
            }else {
                return R.error();
            }
        }
    }
}
